<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入任务名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isToTop">
        <el-select v-model="queryParams.isToTop" placeholder="请选择是否置顶" clearable>
          <el-option v-for="e in isToTopList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式" prop="contactInformation">
        <el-input v-model="queryParams.contactInformation" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="审核状态" prop="reviewStatus">
        <el-select v-model="queryParams.reviewStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="e in reviewStatusList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属类目" prop="reviewStatus">
        <el-select v-model="queryParams.item" placeholder="请选择所属类目" clearable>
          <el-option v-for="e in sysItemList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['task:task:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['task:task:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['task:task:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['task:task:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="任务编号" align="center" prop="id" width="100"/>
      <el-table-column label="任务名称" align="center" prop="title" width="180">
        <template slot-scope="scope">
          <a @click="handleDetail(scope.row)" style="color: dodgerblue">{{ scope.row.title }}</a>
        </template>
      </el-table-column>
      <el-table-column label="发布人员" align="center" prop="publishUserName"/>
      <el-table-column label="具体任务" align="center" prop="taskInfo" width="100" show-overflow-tooltip/>
      <el-table-column label="任务图片" align="center" prop="taskImage">
        <template slot-scope="scope">
          <image-preview :src="scope.row.taskImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isToTop">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_to_top" :value="scope.row.isToTop"/>
        </template>
      </el-table-column>
      <el-table-column label="所属类目" align="center" prop="item">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_item" :value="scope.row.item"/>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="contactInformation" width="120"/>
      <el-table-column label="审核状态" align="center" prop="reviewStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_review_status" :value="scope.row.reviewStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核人员" align="center" prop="reviewUserName" width="120"/>
      <el-table-column label="驳回原因" align="center" prop="reviewRejectionReason" width="180" show-overflow-tooltip/>
      <el-table-column label="发布时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="审核时间" align="center" prop="reviewTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['task:task:edit']">修改</el-button>
            </el-col>
            <el-col :span="12">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['task:task:remove']">删除</el-button>
            </el-col>
            <el-col :span="12">
              <el-button v-if="scope.row.reviewStatus === 1" size="mini" type="text" icon="el-icon-view" @click="handleReview(scope.row)" v-hasPermi="['task:task:review']">审核</el-button>
            </el-col>
            <el-col :span="12">
              <el-button v-if="scope.row.type === '2'" size="mini" type="text" icon="el-icon-star-off" @click="handleCancelFeatured(scope.row)" v-hasPermi="['task:task:cancelFeatured']">取消精选</el-button>
            </el-col>
            <el-col :span="12">
              <el-button v-if="scope.row.isToTop === '0'" size="mini" type="text" icon="el-icon-caret-top" @click="handleMarkTop(scope.row)" v-hasPermi="['task:task:markTop']">标记置顶</el-button>
            </el-col>
            <el-col :span="12">
              <el-button v-if="scope.row.isToTop === '1'" size="mini" type="text" icon="el-icon-caret-bottom" @click="handleCancelTop(scope.row)" v-hasPermi="['task:task:cancelTop']">取消置顶</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="任务名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入任务名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="具体任务" prop="taskInfo">
              <el-input v-model="form.taskInfo" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶" prop="isToTop">
              <el-select v-model="form.isToTop" placeholder="请选择是否置顶">
                <el-option v-for="e in isToTopList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="contactInformation">
              <el-input v-model="form.contactInformation" placeholder="请输入联系方式" maxlength="11"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择任务类型">
                <el-option v-for="e in taskTypeList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属类目" prop="item">
              <el-select v-model="form.item" placeholder="请选择所属类目">
                <el-option v-for="e in sysItemList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务图片" prop="taskImage">
              <ImageUpload v-model="form.taskImage" :limit="5"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog title="审核" :visible.sync="reviewOpen" width="40%" append-to-body @close="cancelReview">
      <el-descriptions title="任务信息" :column="2" :border="true" :label-style="LS">
        <el-descriptions-item label="任务名称" :span="2">{{ reviewForm.title }}</el-descriptions-item>
        <el-descriptions-item label="具体任务" :span="2">{{ reviewForm.taskInfo }}</el-descriptions-item>
        <el-descriptions-item label="创建时间" :span="2">{{ reviewForm.createTime }}</el-descriptions-item>
        <el-descriptions-item label="任务图片" :span="3">
          <!-- 图片文件 -->
          <span v-for="(item,index) of reviewForm.taskImageList" style="margin-left: 5px;">
            <el-image style="width: 100px; height: 100px" :src="item" :preview-src-list="reviewForm.taskImageList" :z-index="9999"/>
          </span>
        </el-descriptions-item>
      </el-descriptions>
      <el-divider/>
      <el-form ref="reviewForm" :model="reviewForm" :rules="reviewRules" label-width="80px">
        <el-form-item label="审核结果" prop="reviewStatus">
          <el-radio-group v-model="reviewForm.reviewStatus">
            <el-radio label="99" border>通过</el-radio>
            <el-radio label="98" border>驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="reviewForm.reviewStatus===98||reviewForm.reviewStatus==='98'" label="驳回原因" prop="reviewRejectionReason">
          <el-input v-model="reviewForm.reviewRejectionReason" type="textarea" placeholder="请输入驳回原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReviewForm">确 定</el-button>
        <el-button @click="cancelReview">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 任务详情组件 -->
    <TaskDetail :task-detail="taskDetail" :detail-open="detailOpen" @cancelDetail="cancelDetail"></TaskDetail>
  </div>
</template>

<script>
import {addTask, cancelFeatured, cancelTop, delTask, getTask, listTaskFeatured, markTop, reviewTask, updateTask} from '@/api/task/task'
import ImageUpload from '@/components/ImageUpload'
import TaskDetail from '@/components/TaskDetail/TaskDetail'

export default {
  components: {ImageUpload, TaskDetail},
  name: 'taskFeatured',
  dicts: ['is_to_top', 'task_type', 'task_review_status', 'sys_item'],
  data() {
    const validMobile = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3456789]\d{9}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      loading: true,                                                        // 遮罩层
      ids: [],                                                              // 选中数组
      single: true,                                                         // 非单个禁用
      multiple: true,                                                       // 非多个禁用
      showSearch: true,                                                     // 显示搜索条件
      total: 0,                                                             // 总条数
      taskList: [],                                                         // 任务表格数据
      title: '',                                                            // 弹出层标题
      open: false,                                                          // 是否显示弹出层
      queryParams: {                                                        // 查询参数
        pageNum: 1,
        pageSize: 10,
        title: null,
        publishUserId: null,
        taskInfo: null,
        taskImage: null,
        taskText: null,
        type: 2,
        item: null,
        isToTop: null,
        contactInformation: null,
        reviewUserId: null,
        reviewStatus: null,
        reviewRejectionReason: null,
        reviewTime: null
      },
      taskDetail: {},
      detailOpen: false,
      form: {                                                               // 表单参数
      },
      rules: {                                                              // 表单校验
        title: [{required: true, message: '请输入任务名称', trigger: 'blur'}],
        taskInfo: [{required: true, message: '请输入具体任务', trigger: 'blur'}],
        isToTop: [{required: true, message: '请选择是否置顶', trigger: 'blur'}],
        contactInformation: [{required: true, message: '请输入联系方式', trigger: 'blur'}, {validator: validMobile, trigger: 'blur'}],
        type: [{required: true, message: '请选择任务类型', trigger: 'blur'}],
        item: [{required: true, message: '请选择所属类目', trigger: 'blur'}]
      },
      isToTopList: [],                                                      // 是否置顶列表
      reviewStatusList: [],                                                 // 审核状态列表
      sysItemList: [],                                                      // 任务类目列表
      taskTypeList: [],
      reviewOpen: false,                                                    // 审核开关
      reviewForm: {                                                         // 审核表单
        id: null,
        title: null,
        taskInfo: null,
        taskImage: null,
        taskImageList: [],
        createTime: null,
        reviewStatus: null,
        reviewRejectionReason: null
      },
      reviewRules: {                                                        // 审核表单校验
        reviewStatus: [{required: true, message: '请选择审核结果', trigger: 'blur'}],
        reviewRejectionReason: [{required: true, message: '请输入驳回原因', trigger: 'blur'}]
      },
      LS: {
        'min-width': '110px',
        'word-break': 'keep-all'
      }
    }
  },
  created() {
    this.initDict()
    this.getList()
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true
      listTaskFeatured(this.queryParams).then(response => {
        this.taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        publishUserId: null,
        taskInfo: null,
        taskImage: null,
        taskText: null,
        type: null,
        item: null,
        isToTop: null,
        contactInformation: null,
        reviewUserId: null,
        reviewStatus: null,
        reviewRejectionReason: null,
        createTime: null,
        updateTime: null,
        reviewTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加任务'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改任务'
      })
    },
    /** 详情按钮操作 */
    handleDetail: function (row) {
      getTask(row.id).then(response => {
        this.taskDetail = response.data
        this.detailOpen = true
      })
    },
    /** 退出详情组件操作 */
    cancelDetail: function (detailOpen) {
      this.detailOpen = detailOpen
      this.taskDetail = {}
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function () {
        return delTask(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 取消精选按钮操作 */
    handleCancelFeatured(row) {
      cancelFeatured(row.id).then(response => {
        this.$modal.msgSuccess('标记成功')
        this.getList()
      })
    },
    /** 标记置顶按钮操作 */
    handleMarkTop(row) {
      markTop(row.id).then(response => {
        this.$modal.msgSuccess('置顶成功')
        this.getList()
      })
    },
    /** 取消置顶按钮操作 */
    handleCancelTop(row) {
      cancelTop(row.id).then(response => {
        this.$modal.msgSuccess('取消置顶成功')
        this.getList()
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    /** 审核按钮操作 */
    handleReview: function (row) {
      this.reviewOpen = true
      this.reviewForm = {
        id: row.id,
        title: row.title,
        taskInfo: row.taskInfo,
        taskImage: row.taskImage,
        createTime: row.createTime,
        taskImageList: row.taskImageList
      }
    },
    /** 提交审核操作 */
    submitReviewForm: function () {
      this.$refs['reviewForm'].validate(valid => {
        if (valid) {
          reviewTask(this.reviewForm).then(response => {
            this.$modal.msgSuccess('操作成功')
            this.reviewOpen = false
            this.getList()
          })
        }
      })
    },
    /** 退出审核操作 */
    cancelReview: function () {
      this.reviewOpen = false
      this.resetReviewForm()
    },
    /** 审核表单重置操作 */
    resetReviewForm() {
      this.reviewForm = {
        id: null,
        title: null,
        taskInfo: null,
        taskImage: null,
        taskImageList: [],
        createTime: null,
        reviewStatus: null,
        reviewRejectionReason: null
      }
      this.resetForm('reviewForm')
    },

    /** 初始化字典 */
    initDict: function () {
      this.getDicts('is_to_top').then(response => {
        this.isToTopList = response.data
      })
      this.getDicts('task_review_status').then(response => {
        this.reviewStatusList = response.data
      })
      this.getDicts('sys_item').then(response => {
        this.sysItemList = response.data
      })
      this.getDicts('task_type').then(response => {
        this.taskTypeList = response.data
      })
    }
  }
}
</script>
